#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"

void QueueInit(Queue* q) {
	assert(q);
	q->front = q->rear = NULL;
	q->size = 0;
}

void QueuePush(Queue* q, QDataType data) {
	assert(q);
	QNode* tmp = (QNode*)malloc(sizeof(QNode));
	if (tmp == NULL) {
		perror("malloc fail");
		exit(-1);
	}
	tmp->data = data;
	tmp->next = NULL;
	if (q->front == NULL) {
		q->front = q->rear = tmp;
	}
	else {
		q->rear->next = tmp;
		q->rear = q->rear->next;
	}
	q->size++;
}


void QueuePop(Queue* q) {
	assert(q);
	assert(!QueueEmpty(q));
	QNode* cur = q->front;
	q->front = q->front->next;
	free(cur);
	cur = NULL;
	q->size--;
}

int QueueSize(Queue* q) {
	assert(q);
	return q->size;
}

QDataType QueueFront(Queue* q) {
	assert(q);
	assert(q->size);
	return q->front->data;
}

QDataType QueueBack(Queue* q) {
	assert(q);
	assert(q->size);
	return q->rear->data;
}

int QueueEmpty(Queue* q) {
	assert(q);
	return q->size == 0;
}

void QueueDestroy(Queue* q) {
	assert(q);
	if (q->front) {
		QNode* cur = q->front;
		while (cur) {
			q->front = q->front->next;
			free(cur);
			cur = q->front;
		}
	}
	q->front = q->rear = NULL;
	q->size = 0;
}